package com.charging.controller.admin;

import com.charging.entity.ChargingPile;
import com.charging.service.ChargingPileService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/pile")
public class PileManageController {

    @Resource
    private ChargingPileService chargingPileService;

    // 列表页
    @GetMapping("/list")
    public String list(Model model) {
        List<ChargingPile> pileList = chargingPileService.findAll();
        model.addAttribute("pileList", pileList);
        return "admin/pile/list";
    }

    // 新增页
    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("pile", new ChargingPile());
        return "admin/pile/add";
    }

    @PostMapping("/add")
    public String add(ChargingPile pile) {
        if (pile.getCreateTime() == null) {
            pile.setCreateTime(new Date()); // 或 LocalDateTime.now()
        }
        if (pile.getUpdateTime() == null) {
            pile.setUpdateTime(new Date()); // 或 LocalDateTime.now()
        }
        chargingPileService.add(pile);
        return "admin/pile/list";
    }

    // 保存（新增/编辑）
    @GetMapping("/edit/{id}")
    public String editPileForm(@PathVariable Long id, Model model) {
        ChargingPile pile = chargingPileService.findById(id);
        model.addAttribute("pile", pile);
        return "admin/pile/edit";
    }

    @PostMapping("/edit")
    public String edit(@ModelAttribute ChargingPile pile) {
        chargingPileService.update(pile);
        return "admin/pile/list";
    }


    @GetMapping("/detail/{id}")
    public String pileDetail(@PathVariable Long id, Model model) {
        model.addAttribute("pile", chargingPileService.findById(id));
        System.out.println("进入预约充电页面方法，充电桩ID：" + id);
        return "admin/pile/detail";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        chargingPileService.delete(id);
        return "admin/pile/list";
    }
}